﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Data.SqlClient;
using System.Data;
using JJY.DBUtility;
namespace Lottery.DBUtility
{
    public class LotterySQLEntityHelper<T> : LotterySQLHelper where T : class
    {
        public static T GetEntity(string sql, params SqlParameter[] commandParameters)
        {
            if (GetEntitySet(sql, commandParameters).Any())
            {
                return GetEntitySet( sql, commandParameters).First();
            }
            return null;
        }

        //public static T GetEntity(string conString, string sql, params SqlParameter[] commandParameters)
        //{
            
        //    if (GetEntitySet(conString, sql, commandParameters).Any())
        //    {
        //        return GetEntitySet(conString, sql, commandParameters).First();
        //    }
        //    return null;
        //}

        public static List<T> GetEntitySet(string sql, params SqlParameter[] commandParameters)
        {
            return GetEntitySet(LotterySQLHelper.ExecuteReader(LotteryApplicationVariable.ConnectionString, CommandType.Text, sql, commandParameters));
        }


        public static List<T> GetEntitySet(CommandType cmdType, string cmdText, params SqlParameter[] commandParameters)
        {
            using (SqlDataReader dr = SQLHelper.ExecuteReader(LotteryApplicationVariable.ConnectionString, cmdType, cmdText, commandParameters))
            {
                return GetEntitySet(dr);
            }
        }

        //public static List<T> GetEntitySet(string conString, string sql, params SqlParameter[] commandParameters)
        //{
        //    return GetEntitySet(LotterySQLHelper.ExecuteReader(conString, CommandType.Text, sql, commandParameters));
        //}

        public static List<T> GetEntitySet(SqlDataReader dr)
        {   
            List<T> result = new List<T>();

            DataView tempView = dr.GetSchemaTable().DefaultView;
            while (dr.Read())
            {
                T temp = Assembly.Load(Assembly.GetAssembly(typeof(T)).FullName).CreateInstance(typeof(T).ToString()) as T;
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    tempView.RowFilter="ColumnName = '" + propertyInfo.Name + "'";
                    if (tempView.Count > 0 &&
                        propertyInfo.PropertyType == dr[propertyInfo.Name].GetType())
                    {
                        propertyInfo.SetValue(temp, dr[propertyInfo.Name], null);
                    }
                }
                result.Add(temp);
            }
            return result;
        }

        public static List<T> GetEntitySet(DataTable dataTable)
        {
            List<T> result = new List<T>();            

            foreach (DataRow dataRow in dataTable.Rows)
            {
                T temp = Assembly.Load(Assembly.GetAssembly(typeof(T)).FullName).CreateInstance(typeof(T).ToString()) as T;
                foreach (PropertyInfo propertyInfo in typeof(T).GetProperties())
                {
                    if (dataTable.Columns.Contains(propertyInfo.Name) &&
                        dataTable.Columns[propertyInfo.Name].DataType == propertyInfo.PropertyType &&
                        !(dataRow[propertyInfo.Name] is DBNull))
                    {
                        propertyInfo.SetValue(temp, dataRow[propertyInfo.Name], null);
                    }
                }
                result.Add(temp);
            }

            return result;
        }
    }
}